//
//  PTDiaMobile.swift
//  PointTest28
//
//  Created by sc-chen on 2023/6/25.
//

import SwiftUI

struct PTDiaMobile: View {
    @State private var mobile = ""
    @State private var mobile_zhuan = ""
    
    @State private var isShowingHUD = false
    @State private var detailTxt = "暂不支持"
    
    var body: some View {
        ZStack {
            ScrollView {
                VStack {
                    Text("请输入电话号码")
                        .frame(maxWidth: .infinity, alignment: .topLeading)
                        .padding(.leading,10)
                    TextField("请输入", text: $mobile)
                        
                        .overlay(
                            RoundedRectangle(cornerRadius: 5)
                                .stroke(Color.gray, lineWidth: 1)
                                .frame(height: 40)
                        )
                        .padding(.leading,10)
                        .padding(.trailing,10)
                        .submitLabel(.done)
                    
                    VStack {
                        Button {
                            makeSysDia()
                        } label: {
                            Text("系统拨号")
                                .foregroundColor(.white)
                                .padding()
                                .frame(maxWidth: .infinity,maxHeight: 40)
                                .background(
                                    RoundedRectangle(cornerRadius: 25)
                                        .fill(Color.blue)
                                )
                        }
                        .buttonStyle(.plain)
                        .padding()
                    }
                    
                    Text("JAVA反射拨号")
                        .foregroundColor(.white)
                        .padding()
                        .frame(maxWidth: .infinity,maxHeight: 40)
                        .background(
                            RoundedRectangle(cornerRadius: 25)
                                .fill(Color.blue)
                                .padding(.leading, 40)
                                .padding(.trailing, 40)
                        )
                        .onTapGesture {
                            //showHud(text: "暂不支持", hudMode: .text)
                        }
                        .hidden()
                    
                    Text("呼叫转移号码")
                        .frame(maxWidth: .infinity, alignment: .topLeading)
                        .padding(.leading,10)
                    
                    Text("不支持呼叫转移号码的开发功能")
                        .padding(.top,20)
                    
                    /*TextField("请输入", text: $mobile_zhuan)
                        .padding()
                        .overlay(
                            RoundedRectangle(cornerRadius: 5)
                                .stroke(Color.gray, lineWidth: 1)
                                .frame(height: 40)
                        )
                        .padding(.leading,10)
                        .padding(.trailing,10)
                        .submitLabel(.done)
                        .onTapGesture {
                            showHud()
                        }
                    
                    VStack {
                        Button {
                            showHud()
                        } label: {
                            Text("确定")
                                .foregroundColor(.white)
                                .padding()
                                .frame(maxWidth: .infinity,maxHeight: 40)
                                .background(
                                    RoundedRectangle(cornerRadius: 25)
                                        .fill(Color.blue)
                                )
                        }
                        .buttonStyle(.plain)
                        .padding()
                    }
                    .padding(.bottom,10)
                    .padding(.top,30)
                    
                    Button {
                        showHud()
                    } label: {
                        Text("取消呼叫转移")
                            .foregroundColor(.white)
                            .padding()
                            .frame(maxWidth: .infinity,maxHeight: 40)
                            .background(
                                RoundedRectangle(cornerRadius: 25)
                                    .fill(Color.blue)
                            )
                    }
                    .buttonStyle(.plain)
                    .padding()*/
                    Spacer()
                }
            }
            PTProgressHud(isPresented: $isShowingHUD, text: $detailTxt)
        }
    }
    
    func makeSysDia() -> Void {
        if mobile.isEmpty {
            detailTxt = "请输入有效的电话号码"
            showHud()
            return
        }
        guard let phoneURL = URL(string: "tel://\(mobile)") else {
            return
        }
        WKExtension.shared().openSystemURL(phoneURL)
    }
    
    private func showHud() {
        isShowingHUD = true
        DispatchQueue.main.asyncAfter(deadline: .now()+2){
            isShowingHUD = false
        }
    }
}

struct PTDiaMobile_Previews: PreviewProvider {
    static var previews: some View {
        PTDiaMobile()
    }
}
